/*
 * @Author: liangyjian 
 * @Date: 2020-09-91 12:08:11 
 * @Last Modified by:   liangyijian 
 * @Last Modified time: 2020-09-91 12:08:11 
 */
<template>
    <el-container style="height: 550px; border: 1px solid #eee">
        <el-main style="overflow: hidden">
            <table style="width: 100%; font-size: 12px">
                <tr v-if="germmode">
                    <td style="height: 26px; padding-left: 60px" colspan="4">
                        <el-radio-group @change="germflagchange" v-model="germflag">
                            <el-radio :label="4">涂片结果</el-radio>
                            <el-radio :label="1">阴性结果</el-radio>
                            <el-radio :label="2">致病菌</el-radio>
                            <el-radio :label="3">正常菌种</el-radio>
                            <el-radio :label="5">细菌其他</el-radio>
                            <el-radio :label="6">基因项目</el-radio>
                            <el-radio :label="99">全部</el-radio>
                        </el-radio-group>
                    </td>
                </tr>
                <tr v-else>
                    <td style="max-width: 60px; min-width: 60px">报告单元</td>
                    <td style="width: 50%">
                        <rm-ddl-rptunit :prgno="prgno" :appendtobody="true" :disabled="rptunitdisabled" @change="handlerptunitchange" v-model="myrptunitid" width="100%"></rm-ddl-rptunit>
                    </td>
                    <td style="max-width: 60px; min-width: 60px">项目分类</td>
                    <td style="width: 50%">
                        <rm-ddl-rptitemclass @change="handleitemclasschange" v-model="myitemclass" width="100%"></rm-ddl-rptitemclass>
                    </td>
                </tr>
                <tr>
                    <td colspan="4">
                        <el-input @keyup.native="searchkeydown" @input="autosearch" placeholder="快速检索" prefix-icon="el-icon-search" ref="qcodeinput" style="width: 300px" v-model="qcode"></el-input>
                        <el-button @click="btncancel" icon="el-icon-circle-close" style="float: right; margin-left: 10px" type="primary">取消</el-button>
                        <el-button @click="btnok" icon="el-icon-circle-check" style="float:right; margin -right:10px;" type="primary">确定</el-button>
                        <el-button @click="refreshdata" icon="el-icon-refresh" style="float: right" type="primary">刷新数据 </el-button>
                    </td>
                </tr>
            </table>
            <vxe-grid :data="tableData" :loading="loading" :sort-config="{ trigger: 'cell' }" @cell-dblclick="vxselect" border fit height="488px" header-align="center" highlight-current-row highlight-hover-row highlight-hover-column ref="ddlrptitemTable" resizable show-header-overflow="tooltip" show-overflow="tooltip" size="mini" stripe :columns="tableColumn">
                <!-- <vxe-table-column type="selection" width="50" align="center"></vxe-table-column> -->
                <!--<vxe-table-column :field="items.field" :key="items.field" :title="items.title" :width="items.width" sortable v-for="items in tableColumn"></vxe-table-column> -->
            </vxe-grid>
        </el-main>
    </el-container>
</template>
<script>
export default {
    props: ["rptunitid", "germmode", "isAllRpt", "itemclass", "rptunitdisabled", "prgno", "initgermflag", "fitembyrptunit"],
    data() {
        return {
            fulldata: null,
            myrptunitid: this.rptunitid,
            myitemclass: this.itemclass,
            germflag: this.initgermflag ? this.initgermflag : 99,
            selrow: null,
            qcode: "",
            clsMenus: [],
            tableColumn: [],
            tablePage: {
                pageSize: 1000,
            },
            tableData: [],
            seloptions: [],
            loading: false,
        };
    },
    computed: {},
    updated() {
        this.$nextTick(() => {
            this.rptunitid = this.rptunitid;
            //this.myrptunitid = this.rptunitid; //加上后报告单元不可选择，
        });
    },
    methods: {
        autosearch: function (evt) {
            if (evt.keyCode != 13 && evt.keyCode != 40 && evt.keyCode != 38) {
                this.filterdata();
            }
        },
        germflagchange: function () {
            this.filterdata();
        },
        setcol() {
            if (this.germmode) {
                this.tableColumn = [
                    { type: "selection", width: 50, align: "center" },
                    { field: "itemcode_en", title: "英文缩写", width: 100, sortable: true },
                    {
                        field: "rpt_itemname",
                        title: "细菌项目名称",
                        width: 200,
                        sortable: true,
                    },
                    { field: "itemname_en", title: "英文名称", width: 200, sortable: true },
                    { field: "seq", title: "使用频次", width: 60, sortable: true },
                    {
                        field: "isgermgroup",
                        title: "是否菌属",
                        width: 100,
                        sortable: true,
                        formatter: function ({ cellValue, row, rowIndex, column, columnIndex }) {
                            if (cellValue == "1") return "是";
                            else return "";
                        },
                    },
                    {
                        field: "groupitemcode",
                        title: "所属菌属",
                        width: 100,
                        sortable: true,
                    },
                    {
                        field: "rpt_itemcode",
                        title: "项目代码",
                        width: 100,
                        sortable: true,
                    },
                    { field: "inputcode", title: "助记符", width: 100, sortable: true },
                ];
            } else {
                this.tableColumn = [
                    { type: "selection", width: 50, align: "center" },
                    {
                        field: "itemclassname",
                        title: "项目归类",
                        width: 100,
                        sortable: true,
                    },
                    {
                        field: "rpt_itemcode",
                        title: "项目代码",
                        width: 100,
                        sortable: true,
                    },
                    { field: "itemcode_en", title: "英文缩写", width: 100, sortable: true },
                    {
                        field: "rpt_itemname",
                        title: "报告项目名称",
                        width: 200,
                        sortable: true,
                    },
                    { field: "unit", title: "单位", width: 100, sortable: true },
                    { field: "ref_text", title: "参考值", width: 200, sortable: true },
                    { field: "testmethod", title: "方法学", width: 200, sortable: true },
                    { field: "itemtips", title: "项目备注", width: 200, sortable: true },
                    {
                        field: "instrclassname",
                        title: "仪器类别",
                        width: 100,
                        sortable: true,
                    },
                    { field: "inputcode", title: "助记符", width: 100, sortable: true },
                ];
            }
        },
        changeactiverow: function (cindex) {
            var row = this.$refs.ddlrptitemTable.getCurrentRow();
            var ridx = this.$refs.ddlrptitemTable.getRowIndex(row);
            var newidx = ridx + cindex;
            if (newidx < 0) newidx = 0;
            if (newidx >= this.tableData.length) {
                newidx = 0;
            }
            var newrow = this.tableData[newidx];
            this.$refs.ddlrptitemTable.setCurrentRow(newrow);
            this.$nextTick(() => {
                if (this.$refs.ddlrptitemTable) {
                    var crow = this.$refs.ddlrptitemTable.getCurrentRow();
                    this.$refs.ddlrptitemTable.scrollToRow(crow);
                }
            });
        },
        vxselect: function (data, evt) {
            this.$emit("selitem", data.row.rpt_itemcode, data.row.rpt_itemname, data.row);
        },
        searchkeydown: function (evt) {
            if (evt.keyCode == 13) {
                let row = this.$refs.ddlrptitemTable.getCurrentRow();
                this.$emit("selitem", row.rpt_itemcode, row.rpt_itemname, row);
            } else if (evt.keyCode == 40) {
                var cindex = 1;
                this.changeactiverow(cindex);
            } else if (evt.keyCode == 38) {
                var cindex = -1;
                this.changeactiverow(cindex);
            }
        },
        handlerptunitchange: function (val) {
            this.myrptunitid = val;
            this.filterdata();
        },
        handleitemclasschange: function (val, sobj) {
            this.myitemclass = val;
            this.filterdata();
        },
        async filterdata() {
            try {
                if (this.germmode) {
                    var cData = [];
                    if (this.fitembyrptunit == "1") {
                        cData = await this.$ddldata.getrptunititem(this.myrptunitid);
                    } else {
                        cData = await this.$ddldata.getallrptitem();
                    }
                    if (this.germflag == 99) {
                        cData = cData.filter((item) => item.germflagid != "0");
                    } else {
                        cData = cData.filter((item) => item.germflagid == this.germflag);
                    }

                    if (this.qcode != "") {
                        cData = cData.filter((item) =>(!!item.rpt_itemcode &&  item.rpt_itemcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.rpt_itemname && item.rpt_itemname.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.inputcode && item.inputcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.itemcode_en && item.itemcode_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1));
                    }
                    if (cData.length > 0) {
                        this.selrow = cData[0];
                    } else {
                        this.selrow == null;
                    }
                    cData = cData.sort((a, b) => b.seq - a.seq);
                    this.tableData = cData;
                    this.$nextTick(() => {
                        if (this.$refs.ddlrptitemTable) {
                            if (this.selrow) {
                                this.$refs.ddlrptitemTable.setCurrentRow(this.selrow);
                            }
                        }
                    });
                } else {
                    if (this.myrptunitid && this.myrptunitid != 0) {
                        var cData = await this.$ddldata.getrptunititem(this.myrptunitid);
                    } else {
                        cData = await this.$ddldata.getitem(this.isAllRpt);
                    }
                    if (this.myitemclass && this.myitemclass != "") {
                        cData = cData.filter((item) => item.itemclasscode == this.myitemclass || item.parentclasscode == this.myitemclass);
                    }
                    if (this.qcode != "") {
                        cData = cData.filter((item) =>(!!item.rpt_itemcode &&  item.rpt_itemcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.rpt_itemname && item.rpt_itemname.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.inputcode && item.inputcode.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1) || (!!item.itemcode_en && item.itemcode_en.toUpperCase().indexOf(this.qcode.toUpperCase()) > -1));
                    }

                    if (cData.length > 0) {
                        this.selrow = cData[0];
                    } else {
                        this.selrow == null;
                    }
                    this.tableData = cData;
                    this.$nextTick(() => {
                        if (this.$refs.ddlrptitemTable) {
                            if (this.selrow) {
                                this.$refs.ddlrptitemTable.setCurrentRow(this.selrow);
                            }
                        }
                    });
                }
            } catch (e) {
                console.log("erro", e);
            }
        },
        findname(code) {
            let name = code;
            if (code != "") {
                if (this.$ddldata.rmrptitem) {
                    let cData = this.$ddldata.rmrptitem;
                    cData = cData.filter((item) => item.rpt_itemcode === code);
                    if (cData.length > 0) {
                        name = cData[0].rpt_itemname;
                    }
                }
            }
            return name;
        },
        refreshdata: function () {
            this.$ddldata.rmallrptitem = "";
            this.$ddldata.rmrptitem = "";
            this.$ddldata.rmrptunititem = [];
            this.loadAllData();
        },
        async loadAllData() {
            this.loading = true;
            var res = await this.$ddldata.getrmrptitem();
            this.filterdata();
            this.loading = false;
        },
        btncancel() {
            this.$emit("cancel");
        },
        btnok: function () {
            let rows = this.$refs.ddlrptitemTable.getSelectRecords();
            this.$emit("selitems", rows);
        },
    },
    mounted() {
        this.loadAllData();
        this.setcol();
    },
    watch: {
        rptunitid(val) {
            if (val == "") {
                this.myrptunitid = 0;
            } else {
                if (isNaN(val)) {
                    this.myrptunitid = 0;
                } else {
                    var intval = parseInt(val);
                    this.myrptunitid = intval;
                }
            }
            this.filterdata();
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0;
}
</style>
bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb